﻿namespace CoreClass
{
    using System;
    using System.Collections;
    using System.Data;
    using System.Data.SqlClient;
    using System.Web.UI;

    public class Business
    {
        private DBCommon _dbc = new DBCommon();

        public bool BankLog(int hy_num, decimal money, string bz)
        {
            string sql = "Insert Into [Bank_Log]([hy_num],[Balance],[bz]) values(@num,@balance,@bz)";
            SqlCommand cmd = this.dbc.GetCommandBySql(sql);
            cmd.Parameters.Add("@num", SqlDbType.Int).Value = hy_num;
            cmd.Parameters.Add("@balance", SqlDbType.Money).Value = money;
            cmd.Parameters.Add("@bz", SqlDbType.VarChar, 0x100).Value = bz;
            try
            {
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                cmd.Connection.Close();
                cmd.Connection.Dispose();
                cmd.Dispose();
                return true;
            }
            catch
            {
                cmd.Connection.Dispose();
                cmd.Dispose();
                return false;
            }
        }

        public bool BankLog_tx(int hy_num, int tx_id, decimal money, string bz)
        {
            string sql = "Insert Into [Bank_Log]([hy_num],[Balance],[bz],[tx_id]) values(@num,@balance,@bz,@tx_id)";
            SqlCommand cmd = this.dbc.GetCommandBySql(sql);
            cmd.Parameters.Add("@num", SqlDbType.Int).Value = hy_num;
            cmd.Parameters.Add("@ti_id", SqlDbType.Int).Value = tx_id;
            cmd.Parameters.Add("@balance", SqlDbType.Money).Value = money;
            cmd.Parameters.Add("@bz", SqlDbType.VarChar, 0x100).Value = bz;
            try
            {
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                cmd.Connection.Close();
                cmd.Connection.Dispose();
                cmd.Dispose();
                return true;
            }
            catch
            {
                cmd.Connection.Dispose();
                cmd.Dispose();
                return false;
            }
        }

        public bool BankLog_updatebz(int id, int hy_num, string bz)
        {
            string sql = "update [Bank_Log] set bz=@bz where tx_id=@id and hy_num=@num ";
            SqlCommand cmd = this.dbc.GetCommandBySql(sql);
            cmd.Parameters.Add("@num", SqlDbType.Int).Value = hy_num;
            cmd.Parameters.Add("@id", SqlDbType.Int).Value = id;
            cmd.Parameters.Add("@bz", SqlDbType.VarChar, 0x100).Value = bz;
            try
            {
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                cmd.Connection.Close();
                cmd.Connection.Dispose();
                cmd.Dispose();
                return true;
            }
            catch
            {
                cmd.Connection.Dispose();
                cmd.Dispose();
                return false;
            }
        }

        public bool BankOperation(int hy_num, decimal money)
        {
            return this.BankOperation(hy_num, money, false);
        }

        public bool BankOperation(int hy_num, decimal money, bool add)
        {
            string sql = "";
            if (money == 0M)
            {
                return true;
            }
            if (money < 0M)
            {
                money = Math.Abs(money);
                if (this.getUserBalance(hy_num) < money)
                {
                    return false;
                }
                sql = "Update [Bank] Set [Output_Money]=[Output_Money]+" + money.ToString() + ", [Balance]=[Balance]-" + money.ToString() + " where hy_num=" + hy_num.ToString() + " and zt='q'";
                return (this.dbc.ExcuteSqlCount(sql) > 0);
            }
            if (add)
            {
                sql = "Update [Bank] Set [Input_Money]=[Input_Money]+" + money.ToString() + ", [Balance]=[Balance]+" + money.ToString() + " where hy_num=" + hy_num.ToString();
            }
            else
            {
                sql = "Update [Bank] Set [Output_Money]=[Output_Money]-" + money.ToString() + ", [Balance]=[Balance]+" + money.ToString() + " where hy_num=" + hy_num.ToString();
            }
            return (this.dbc.ExcuteSqlCount(sql) > 0);
        }

        public bool BankOperation(int hy_num, decimal money, string bz)
        {
            if (this.BankOperation(hy_num, money, false))
            {
                if (this.BankLog(hy_num, money, bz))
                {
                    return true;
                }
              //  this.BankOperation(hy_num, decimal.op_UnaryNegation(money), false);
                return false;
            }
            return false;
        }

        public bool BankOperation(int hy_num, decimal money, bool add, string bz)
        {
            if (this.BankOperation(hy_num, money, add))
            {
                if (this.BankLog(hy_num, money, bz))
                {
                    return true;
                }
               // this.BankOperation(hy_num, decimal.op_UnaryNegation(money), add);
                return false;
            }
            return false;
        }

        public ArrayList BuyHmFangAn(int hy_num, string hy_name, int fid, int fenshu)
        {
            ArrayList al = new ArrayList();
            al.Add(false);
            al.Add("");
            if (fenshu < 1)
            {
                al[1] = "\u5fc5\u987b\u8d2d\u4e701\u4efd\u4ee5\u4e0a\uff01";
                return al;
            }
            DataTable dtfa = this.dbc.GetDataTableBySql("select top 1 id,CaiZhong_id,QiHao,FaPrice,FenShu,FenShu2,ExpTime,chuli from [Hm_FangAn] where [id]=" + fid.ToString() + "");
            if (dtfa.Rows.Count < 1)
            {
                al[1] = "\u8d2d\u4e70\u65b9\u6848\u4e0d\u5b58\u5728\uff01";
                return al;
            }
            int czid = Convert.ToInt32(dtfa.Rows[0]["CaiZhong_Id"]);
            string qh = dtfa.Rows[0]["QiHao"].ToString();
            int fafenshu = Convert.ToInt32(dtfa.Rows[0]["FenShu"]);
            int fafenshu2 = Convert.ToInt32(dtfa.Rows[0]["FenShu2"]);
            if ((fafenshu - fafenshu2) < fenshu)
            {
                al[1] = "\u65b9\u6848\u5269\u4f59\u4efd\u6570\u4e0d\u8db3\uff01";
                return al;
            }
            if (Convert.ToDateTime(dtfa.Rows[0]["ExpTime"]) < DateTime.Now)
            {
                al[1] = "\u65b9\u6848\u8ba4\u8d2d\u65f6\u95f4\u5df2\u8fc7\uff01";
                return al;
            }
            if (Convert.ToBoolean(dtfa.Rows[0]["chuli"]))
            {
                al[1] = "\u65b9\u6848\u5df2\u7ecf\u505c\u6b62\u8ba4\u8d2d\uff01";
                return al;
            }
            decimal reqmoney = fenshu * Convert.ToDecimal(dtfa.Rows[0]["FaPrice"]);
          //  if (this.BankOperation(hy_num, decimal.op_UnaryNegation(reqmoney)))
            if(true)
            {
                string sql = "Update Hm_FangAn Set FenShu2 = FenShu2 + " + fenshu.ToString() + " Where [Id]=" + fid.ToString();
                this.dbc.ExcuteSql(sql);
                sql = "Insert Into Hm_Infos(FangAn_Id,Hy_Num,Hy_Name,FenShu,Price,Amt) Values(" + fid.ToString() + "," + hy_num.ToString() + ",'" + hy_name + "'," + fenshu.ToString() + "," + dtfa.Rows[0]["FaPrice"].ToString() + "," + reqmoney.ToString() + ")";
                this.dbc.ExcuteSql(sql);
             //   this.BankLog(hy_num, decimal.op_UnaryNegation(reqmoney), "\u8ba4\u8d2d\u5408\u4e70\u65b9\u6848" + fenshu.ToString() + "\u4efd\uff0c\u65b9\u6848\u7f16\u53f7\uff1a" + fid.ToString());
                al[0] = true;
                al[1] = "\u6210\u529f\u8ba4\u8d2d\u5408\u4e70\u65b9\u6848\uff08\u7f16\u53f7\uff1a" + fid.ToString() + "\uff09" + fenshu.ToString() + "\u4efd";
                return al;
            }
            al[1] = "\u8ba4\u8d2d\u6b3e\u6263\u9664\u5931\u8d25\uff01\u53ef\u80fd\u4f60\u7684\u5e10\u6237\u4f59\u989d\u4e0d\u591f\uff01";
            return al;
        }

        public int calcXingY(decimal money)
        {
            if (money < 100M)
            {
                return 0;
            }
            if (money < 1000M)
            {
                return 20;
            }
            if (money < 10000M)
            {
                return 50;
            }
            if (money < 100000M)
            {
                return 100;
            }
            if (money < 1000000M)
            {
                return 200;
            }
            if (money < 2000000M)
            {
                return 300;
            }
            if (money < 4000000M)
            {
                return 400;
            }
            return 500;
        }

        public bool Check_DongJie(int hy_num)
        {
            string strsql = "select zt from Bank where hy_num=" + hy_num;
            DataTable Dt = null;
            Dt = this.dbc.GetDataTableBySql(strsql);
            if ((Dt != null) && (Dt.Rows.Count > 0))
            {
                return (Dt.Rows[0][0].ToString() == "q");
            }
            return false;
        }

        public void checkLogin(Page curPage)
        {
            if (((curPage.Request.Cookies["hy_name"] == null) || (curPage.Request.Cookies["hy_name"].Value == "")) || ((curPage.Request.Cookies["hy_num"] == null) || (curPage.Request.Cookies["hy_num"].Value == "")))
            {
                curPage.Response.Write("<script language=\"javascript\">alert(\"\u60a8\u6ca1\u6709\u767b\u9646\u6216\u662f\u767b\u9646\u8d85\u65f6\u8bf7\u767b\u9646\uff01\");top.location=\"/passport/login.aspx?url=\"+top.location+\"\";</script>");
                curPage.Response.End();
            }
        }

        public bool DaiG_GouMai(string caizhong_id, string hy_num, string hy_name, string qihao, string number, string zhushu, string multiple, string unitprice, string totalprice)
        {
            string strsql = "insert DaiGou (CaiZhong_id,Hy_Num,Hy_Name,QiHao,Number,zhushu,Multiple,unitprice,totalprice)";
            strsql = strsql + " values (@CaiZhong_id,@Hy_Num,@Hy_Name,@QiHao,@Number,@zhushu,@Multiple,@unitprice,@totalprice)";
            SqlCommand Comm = this.dbc.GetCommandBySql();
            Comm.Parameters.Add("@CaiZhong_id", SqlDbType.Int).Value = Convert.ToInt32(caizhong_id);
            Comm.Parameters.Add("@Hy_Num", SqlDbType.Int).Value = hy_num;
            Comm.Parameters.Add("@Hy_Name", SqlDbType.VarChar, 50).Value = hy_name;
            Comm.Parameters.Add("@QiHao", SqlDbType.VarChar, 50).Value = qihao;
            Comm.Parameters.Add("@Number", SqlDbType.VarChar, 500).Value = number;
            Comm.Parameters.Add("@zhushu", SqlDbType.VarChar, 50).Value = zhushu;
            Comm.Parameters.Add("@Multiple", SqlDbType.VarChar, 10).Value = multiple;
            Comm.Parameters.Add("@unitprice", SqlDbType.Money).Value = unitprice;
            Comm.Parameters.Add("@totalprice", SqlDbType.Money).Value = totalprice;
            Comm.CommandText = strsql;
            Comm.Connection.Open();
            try
            {
                Comm.ExecuteNonQuery();
                Comm.Connection.Close();
                return true;
            }
            catch
            {
                Comm.Connection.Close();
                return false;
            }
        }

        public SqlDataReader GetQiHaoInfo(int id)
        {
            return this.dbc.GetDataReaderBySql("select top 1 * from [QiShu] where [Id]=" + id.ToString() + "");
        }

        public SqlDataReader GetQiHaoInfo(int czid, string qh)
        {
            return this.dbc.GetDataReaderBySql("select top 1 * from [QiShu] where [CaiZhong_Id]=" + czid.ToString() + " And [QiHao]='" + qh.ToString() + "'");
        }

        public decimal getUserBalance(int hy_num)
        {
            SqlDataReader dr = this.dbc.GetDataReaderBySql("select top 1 [Balance] From [Bank] where [Hy_Num]=" + hy_num.ToString() + "");
            decimal returnValue = 0M;
            if (dr.Read())
            {
                returnValue = Convert.ToDecimal(dr["Balance"]);
            }
            else
            {
                returnValue = -1M;
            }
            dr.Close();
            return returnValue;
        }

        public int getUserLevel(int scope)
        {
            if (scope < 3)
            {
                return 0;
            }
            if (scope < 10)
            {
                return 1;
            }
            if (scope < 40)
            {
                return 2;
            }
            if (scope < 90)
            {
                return 3;
            }
            if (scope < 150)
            {
                return 4;
            }
            if (scope < 250)
            {
                return 5;
            }
            if (scope < 500)
            {
                return 6;
            }
            if (scope < 0x3e8)
            {
                return 7;
            }
            if (scope < 0x7d0)
            {
                return 8;
            }
            if (scope < 0x1388)
            {
                return 9;
            }
            if (scope < 0x2710)
            {
                return 10;
            }
            if (scope < 0x4e20)
            {
                return 11;
            }
            if (scope < 0xc350)
            {
                return 12;
            }
            if (scope < 0x186a0)
            {
                return 13;
            }
            if (scope < 0x30d40)
            {
                return 14;
            }
            return 15;
        }

        public int getUserLevel(string hy_name)
        {
            int rtn = 0;
            SqlDataReader dr = this.dbc.GetDataReaderBySql("select top 1 Xingyd From User_Extent Where Hy_Name='" + hy_name + "'");
            if (dr.Read())
            {
                rtn = this.getUserLevel(Convert.ToInt32(dr["Xingyd"].ToString()));
            }
            dr.Close();
            return rtn;
        }

        public bool isLogin(Page curPage)
        {
            if ((curPage.Request.Cookies["hy_name"] == null) || (curPage.Request.Cookies["hy_name"].Value == ""))
            {
                return false;
            }
            return true;
        }

        public bool UpdateXingYong(int hy_num, int mark)
        {
            return this.UpdateXingYong(hy_num.ToString(), mark);
        }

        public bool UpdateXingYong(string Hy_Num, int mark)
        {
            if ((Hy_Num == null) || (Hy_Num == ""))
            {
                return false;
            }
            if (mark != 0)
            {
                try
                {
                    return (this.dbc.ExcuteSqlCount("Update User_Extent Set Xingyd = Xingyd+(" + mark.ToString() + ") where Hy_Num=" + Hy_Num + "") > 0);
                }
                catch
                {
                    return false;
                }
            }
            return true;
        }

        public DBCommon dbc
        {
            get
            {
                return this._dbc;
            }
        }
    }
}

